package com.example.myapplication.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.utils.UICache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.song.videoplayer.DemoQSVideoView;

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {
    public DemoQSVideoView demoQSVideoView;
    private JSONArray list;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView videoName;

        public ViewHolder (View view) {
            super(view);
            videoName = (TextView) view.findViewById(R.id.video_list_name);
        }

    }

    public VideoListAdapter(JSONArray list,DemoQSVideoView demoQSVideoView){
        this.list = list;
        this.demoQSVideoView=demoQSVideoView;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_list,parent,false);
        ViewHolder holder = new ViewHolder(view);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.width=Math.round(parent.getWidth()/5);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        int index=position;
        try {
            JSONObject o = list.getJSONObject(position);
            if (!o.getString("url").contains("http")){o.put("url","http://["+ UICache.v6+"]"+o.getString("url"));}
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        demoQSVideoView.setUp(o.getString("url"));
                        demoQSVideoView.play();
                        getListener.onClick(index);
                        notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            if (position == getmPosition()) {
                holder.itemView.setBackgroundColor(Color.BLUE);
            }else{
                holder.itemView.setBackgroundColor(Color.parseColor("#a1b9cc"));
            }
            holder.videoName.setText(o.getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount(){
        return list.length();
    }

    public interface GetListener {

        void onClick(int position);
    }

    private GetListener getListener;

    public void setGetListener(GetListener getListener) {
        this.getListener = getListener;
    }
    private  int mPosition;

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }
}
